package com.tianxiao.faas.application.web.corner;

import com.google.common.collect.Lists;
import com.tianxiao.faas.api.param.corner.UserLogListParam;
import com.tianxiao.faas.api.param.corner.UserLogResult;
import com.tianxiao.faas.biz.corner.service.user.UserService;
import com.tianxiao.faas.biz.service.corner.UserLogService;
import com.tianxiao.faas.common.access.Access;
import com.tianxiao.faas.common.constants.UserLogActionConstants;
import com.tianxiao.faas.common.result.Result;
import com.tianxiao.faas.common.util.DateUtils;
import com.tianxiao.faas.mapper.model.UserLogModel;
import com.tianxiao.faas.mapper.model.UsersModel;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/app/admin/log/")
public class UserLogAdminController {
    @Resource
    private UserLogService userLogService;
    @Resource
    private UserService userService;

    @RequestMapping(value = "/actions")
    @Access(access = {"admin"})
    public Result<String> actions() {
        return Result.builder().success(true).data(UserLogActionConstants.actions);
    }

    @RequestMapping(value = "/list")
    @Access(access = {"admin"})
    public Result<List<UserLogResult>> list(@RequestBody UserLogListParam param) {
        String action = param.getAction();
        Long userId = param.getUserId();
        List<UserLogModel> page = userLogService.page(action, userId, param.getPage(), param.getPageSize());

        List<UserLogResult> results = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page)) {
            Set<Long> collect = page.stream().map(UserLogModel::getUserId).collect(Collectors.toSet());
            List<UsersModel> list = userService.list(Lists.newArrayList(collect));
            Map<Long, UsersModel> usersModelMap = list.stream().collect(Collectors.toMap(UsersModel::getId, s -> s));
            for (UserLogModel log : page) {
                UserLogResult logResult = new UserLogResult();
                logResult.setAction(log.getAction());
                logResult.setActionTime(DateUtils.format(log.getActionTime()));
                logResult.setContent(log.getContent());
                logResult.setId(log.getId());
                UsersModel usersModel = usersModelMap.get(log.getUserId());
                if (usersModel != null) {
                    String name = usersModel.getName();
                    name = name == null ? "" : name;
                    String mobile = usersModel.getMobile();
                    mobile = mobile == null ? "" : mobile;
                    String nickname = usersModel.getNickname();
                    nickname = nickname == null ? "" : nickname;
                    logResult.setUser(name + "/" + mobile + "/" + nickname);
                }
                results.add(logResult);
            }
        }
        Integer count = userLogService.count(action, userId);
        return Result.builder().totalCount(count).data(results).success(true)
                .pageNo(param.getPage()).pageSize(param.getPageSize()).totalPage();
    }
}
